package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.entity.InverterAlarmError;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.InverterAlarmErrorService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/alarm/error")
public class InverterAlarmErrorController {

    @Resource(name = "inverterAlarmErrorService")
    private InverterAlarmErrorService inverterAlarmErrorService;

    @GetMapping
    public Result<Page<InverterAlarmError>> get(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum, String name, String code) throws IframeException {
        Page<InverterAlarmError> page = Page.of(pageNo, pageNum);
        LambdaQueryWrapper<InverterAlarmError> lambdaQueryWrapper = new LambdaQueryWrapper<InverterAlarmError>();
        if (StringUtils.isNotEmpty(name)) {
            lambdaQueryWrapper.like(InverterAlarmError::getName, name);
        }
        if (StringUtils.isNotEmpty(code)) {
            lambdaQueryWrapper.like(InverterAlarmError::getCode, code);
        }
        return Result.ok(inverterAlarmErrorService.page(page, lambdaQueryWrapper));
    }

    @GetMapping("/{id}")
    public Result<InverterAlarmError> get(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(inverterAlarmErrorService.getById(id));
    }

    @PostMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> save(@RequestBody InverterAlarmError inverterAlarmError) throws IframeException {
        return Result.ok(inverterAlarmErrorService.save(inverterAlarmError));
    }

    @PutMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> update(@RequestBody InverterAlarmError inverterAlarmError) throws IframeException {
        return Result.ok(inverterAlarmErrorService.update(inverterAlarmError));
    }

    @DeleteMapping(produces = "application/json;charset=utf-8")
    public Result<Integer> delete(@RequestBody List<String> ids) throws IframeException {
        return Result.ok(inverterAlarmErrorService.delete(ids));
    }

    @DeleteMapping("/{id}")
    public Result<Integer> delete(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(inverterAlarmErrorService.delete(id));
    }
}
